package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.topic.ReadTopicVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.admin.MarketTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MarketTopicServiceImpl implements MarketTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
     * 显示优惠券内容，兼顾查询
     * @param title
     * @param subtitle
     * @param basePageInfo 注意sort
     */
    @Override
    public CommonData<MarketTopic> list(String title, String subtitle, Integer relatedId, BasePageInfo basePageInfo) {
        // 注意sort
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit(), basePageInfo.getSort());

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();

        if (!StringUtils.isEmpty(title)) {
            title = "%" + title.trim() + "%";
            criteria.andTitleLike(title);
        }
        if (!StringUtils.isEmpty(subtitle)) {
            subtitle = "%" + subtitle.trim() + "%";
            criteria.andSubtitleLike(subtitle);
        }
        // 前台查询相关专题时，不需要查询自己
        if (relatedId != null) {
            criteria.andIdNotEqualTo(relatedId);
        }
        criteria.andDeletedEqualTo(false);

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);
        CommonData data = CommonData.data(marketTopicPageInfo);
        return data;
    }

    /**
     * 添加专题，注意参数校验以及专题商品
     * 返回参数多了id, addTime, updateTime三项，
     * 注意：前者需要接收，后两者初始值相同
     * @return
     */
    @Override
    public MarketTopic create(MarketTopic marketTopic) {
        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        // 注意接收主键
        int insertSelective = marketTopicMapper.insertSelective(marketTopic);
        return marketTopic;
    }

    /**
     * 进入编辑页面，显示专题详情
     * 先查topic，再查goods的list
     * @param id
     */
    @Override
    public ReadTopicVO read(Integer id) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria exampleCriteria = marketTopicExample.createCriteria();

        exampleCriteria.andIdEqualTo(id);
        exampleCriteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopicList = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);

        if (marketTopicList.size() != 1) {
            return null;
        }
        MarketTopic marketTopic = marketTopicList.get(0);
        // 获得goods数组，此时marketTopic肯定不为null
        Integer[] goods = marketTopic.getGoods();

        // 如果数组有元素，则执行查询操作
        List<MarketGoods> marketGoods = null;
        if (goods.length != 0) {
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();

            criteria.andIdIn(Arrays.asList(goods));

            marketGoods = marketGoodsMapper.selectByExampleWithBLOBs(marketGoodsExample);
        }

        ReadTopicVO readTopicVO = new ReadTopicVO();
        readTopicVO.setTopic(marketTopic);
        readTopicVO.setGoodsList(marketGoods);

        return readTopicVO;
    }

    /**
     * 编辑专题
     * updateTime需要手动更改
     * @param marketTopic
     * @return marketTopic
     */
    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        int update = marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        return marketTopic;
    }

    /**
     * 删除单个专题
     * @param marketTopic
     */
    @Override
    public int delete(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        int update = marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        return update;
        // int delete = marketTopicMapper.deleteByPrimaryKey(marketTopic.getId());
        // return delete;
    }

    /**
     * 批量删除专题
     * @param ids
     */
    @Override
    public int batchDelete(List<Integer> ids) {
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();

        criteria.andIdIn(ids);
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setDeleted(true);

        int updateByExampleSelective = marketTopicMapper.updateByExampleSelective(marketTopic, marketTopicExample);
        return updateByExampleSelective;

        /*int deleteByExample = marketTopicMapper.deleteByExample(marketTopicExample);

        return deleteByExample;*/
    }


}
